/*
 * Copyright (c) 2016. For Intelligent Group.
 */
package com.intelligent.ispc.r.analysis.spc;

import com.intelligent.ispc.core.dto.SpcConfigDto;
import com.intelligent.ispc.r.analysis.RCommand;
import com.intelligent.ispc.r.dto.AnalysisParamDto;
import com.intelligent.ispc.r.dto.ControlDataDto;
import com.intelligent.ispc.r.utils.REnConnector;
import com.intelligent.ispc.r.utils.RUtils;

import java.util.List;

/**
 * Created by Eligi.Ran on 2016/8/30.
 */
public class CCCommand implements RCommand {

    private AnalysisParamDto analysisParamDto;
    private String scriptPath;
    private ControlDataDto resultDto;
    private List<SpcConfigDto> spcConfigDtoList;
    private String type;
    private int digitNum = 6;
    private CConfiguration cConfiguration;

    /**
     * get control chart command
     *
     * @param scriptPath r script path
     * @param type       control chart type
     */
    public CCCommand(String scriptPath, String type) {
        this.scriptPath = scriptPath;
        this.type = type;
    }

    protected ControlDataDto buildScript() {
        REnConnector con = RUtils.getInstance().getConnector();
        int groupNum = analysisParamDto.getGroupNum();
        double[] dataArray = analysisParamDto.getAnalysisData();
        if (dataArray == null || type == null) {
            return null;
        }

        con.setInput("x", dataArray);
        con.setInput("n", groupNum);
//        Integer tempDigitNum = analysisParamDto.getDigitNum();
//        if (tempDigitNum != null)
//            digitNum = tempDigitNum;
        con.setInput("bitNum", digitNum);
        con.execEval("source(\"" + scriptPath + "\")");
        double[] clValue = con.getOutputDoubleArray("CL");
        double[] claData = con.getOutputDoubleArray("claData");
        while (con.isActive()) {
            con.disconnect();
        }
        RUtils.getSemaphore().release();

        int length = claData.length;
        double[] xPosition = new double[length];
        for (int i = 0; i < length; i++) {
            xPosition[i] = i + 1;
        }
        resultDto = new ControlDataDto();
        resultDto.setDataCount(length);
        resultDto.setY(claData);
        resultDto.setMeanData(clValue);
        resultDto.setX(xPosition);
        if (analysisParamDto.getUslValue() != null) {
            resultDto.setUsl(analysisParamDto.getUslValue());

        }
        if (analysisParamDto.getLslValue() != null) {
            resultDto.setLsl(analysisParamDto.getLslValue());
        }

        double avgValue = clValue[3];
        double sigma = clValue[4] - avgValue;
        cConfiguration.setData(claData);
        resultDto.setRuleResultList(cConfiguration.getRuleList(avgValue, sigma));

        return resultDto;
    }

    @Override
    public void execute() {
        buildScript();
    }

    public AnalysisParamDto getAnalysisParamDto() {
        return analysisParamDto;
    }

    public void setAnalysisParamDto(AnalysisParamDto analysisParamDto) {
        this.analysisParamDto = analysisParamDto;
    }

    public String getScriptPath() {
        return scriptPath;
    }

    public void setScriptPath(String scriptPath) {
        this.scriptPath = scriptPath;
    }

    public ControlDataDto getResultDto() {
        return resultDto;
    }

    public void setResultDto(ControlDataDto resultDto) {
        this.resultDto = resultDto;
    }

    public List<SpcConfigDto> getSpcConfigDtoList() {
        return spcConfigDtoList;
    }

    public void setSpcConfigDtoList(List<SpcConfigDto> spcConfigDtoList) {
        this.spcConfigDtoList = spcConfigDtoList;
    }

    public CConfiguration getCconfiguration() {
        return cConfiguration;
    }

    public void setCconfiguration(CConfiguration cConfiguration) {
        this.cConfiguration = cConfiguration;
    }
}
